function getRoomSortMessage(){
    Ajax({
        url:"http://localhost/hotel/php/roomSortMessage.php",
        success(data){
            sessionStorage.setItem("getRoomSortMessage",data)
            sessionStorage.setItem("currentPage",1)
            data=(JSON.parse(data)).slice(0,3)
            let currentPage=sessionStorage.getItem("currentPage")
            let getRoomSortMessage1=JSON.parse(sessionStorage.getItem("getRoomSortMessage"))
            showPage.innerHTML=`${currentPage}/${Math.ceil(getRoomSortMessage1.length/3)}`
            renderPage(data)
        }
    })
}
getRoomSortMessage()

function renderPage(data){
    const tbody=document.querySelector("tbody")
    tbody.innerHTML = ""
    data.forEach(item=>{
        let str=`
        <tr>
        <td>${item.sid}</td>
        <td><${item.imgurl} style="width:150px;height:120px"></td>
        <td>${item.roomType}</td>
        <td>${item.describe1}</td>
        <td>${item.price}</td>
        <td>
        <button type="button" class="btn btn-warning btn btn-primary btn-lg" data-roomid=${item.sid} id="upd" data-bs-toggle="modal" data-bs-target="#myModal2">
            <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-pencil-square" viewBox="0 0 16 16">
            <path d="M15.502 1.94a.5.5 0 0 1 0 .706L14.459 3.69l-2-2L13.502.646a.5.5 0 0 1 .707 0l1.293 1.293zm-1.75 2.456-2-2L4.939 9.21a.5.5 0 0 0-.121.196l-.805 2.414a.25.25 0 0 0 .316.316l2.414-.805a.5.5 0 0 0 .196-.12l6.813-6.814z"/>
            <path fill-rule="evenodd" d="M1 13.5A1.5 1.5 0 0 0 2.5 15h11a1.5 1.5 0 0 0 1.5-1.5v-6a.5.5 0 0 0-1 0v6a.5.5 0 0 1-.5.5h-11a.5.5 0 0 1-.5-.5v-11a.5.5 0 0 1 .5-.5H9a.5.5 0 0 0 0-1H2.5A1.5 1.5 0 0 0 1 2.5v11z"/>
            </svg>修改</button>
        <button type="button" class="btn btn-danger btn btn-primary btn-lg" data-roomid=${item.sid} id="del">
            <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-trash" viewBox="0 0 16 16">
            <path d="M5.5 5.5A.5.5 0 0 1 6 6v6a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5zm2.5 0a.5.5 0 0 1 .5.5v6a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5zm3 .5a.5.5 0 0 0-1 0v6a.5.5 0 0 0 1 0V6z"/>
            <path fill-rule="evenodd" d="M14.5 3a1 1 0 0 1-1 1H13v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V4h-.5a1 1 0 0 1-1-1V2a1 1 0 0 1 1-1H6a1 1 0 0 1 1-1h2a1 1 0 0 1 1 1h3.5a1 1 0 0 1 1 1v1zM4.118 4 4 4.059V13a1 1 0 0 0 1 1h6a1 1 0 0 0 1-1V4.059L11.882 4H4.118zM2.5 3V2h11v1h-11z"/>
            </svg>删除</button>
        </td>
        </tr>
        `
        tbody.innerHTML+=str
    })
}

function first(){
    sessionStorage.setItem("currentPage",1)
    let getRoomSortMessage=JSON.parse(sessionStorage.getItem("getRoomSortMessage"))
     getRoomSortMessage=getRoomSortMessage.slice(0,3)
     let getRoomSortMessage1=JSON.parse(sessionStorage.getItem("getRoomSortMessage"))
    let currentPage=+sessionStorage.getItem("currentPage")
    two.disabled=true
    two.style.backgroundColor="grey"
    three.style.backgroundColor="#31d2f2"
    three.disabled=false
    showPage.innerHTML=`${currentPage}/${Math.ceil(getRoomSortMessage1.length/3)}`
    renderPage(getRoomSortMessage)
}

function down(){
    let currentPage=+sessionStorage.getItem("currentPage")
    let getRoomSortMessage=JSON.parse(sessionStorage.getItem("getRoomSortMessage"))
    let getRoomSortMessage1=JSON.parse(sessionStorage.getItem("getRoomSortMessage"))
    currentPage=currentPage+1
    sessionStorage.setItem("currentPage",currentPage)
        if(currentPage>=Math.ceil(getRoomSortMessage.length/3)){
            currentPage=Math.ceil(getRoomSortMessage.length/3)
            three.style.backgroundColor="grey"
            three.disabled=true
            two.style.backgroundColor="#31d2f2"
            two.disabled=false
        }else{
            two.style.backgroundColor="#31d2f2"
            two.disabled=false
        }
    
    showPage.innerHTML=`${currentPage}/${Math.ceil(getRoomSortMessage1.length/3)}`
    getRoomSortMessage=getRoomSortMessage.slice((currentPage-1)*3,currentPage*3)
    renderPage(getRoomSortMessage)
}

function up(){
    let currentPage=+sessionStorage.getItem("currentPage")
    let getRoomSortMessage1=JSON.parse(sessionStorage.getItem("getRoomSortMessage"))
    currentPage=currentPage-1
    if(currentPage<=1){
        currentPage=1
        two.style.backgroundColor="grey"
        two.disabled=true
        three.style.backgroundColor="#31d2f2"
        three.disabled=false
    }else{
        three.style.backgroundColor="#31d2f2"
        three.disabled=false
    }
    sessionStorage.setItem("currentPage",currentPage)
    let getRoomSortMessage=JSON.parse(sessionStorage.getItem("getRoomSortMessage"))
    showPage.innerHTML=`${currentPage}/${Math.ceil(getRoomSortMessage1.length/3)}`
    getRoomSortMessage=getRoomSortMessage.slice((currentPage-1)*3,currentPage*3)
    renderPage(getRoomSortMessage)
}
function last(){
let getRoomSortMessage=JSON.parse(sessionStorage.getItem("getRoomSortMessage"))
let getRoomSortMessage1=JSON.parse(sessionStorage.getItem("getRoomSortMessage"))
currentPage=Math.ceil((getRoomSortMessage.length)/3)
sessionStorage.setItem("currentPage",currentPage)
if(getRoomSortMessage.length%3!=0){
    getRoomSortMessage=getRoomSortMessage.slice(Math.floor(getRoomSortMessage.length/3)*3,getRoomSortMessage.length)
}else{
    getRoomSortMessage=getRoomSortMessage.slice(Math.floor(getRoomSortMessage.length/3-1)*3,getRoomSortMessage.length)
}
three.disabled=true
three.style.backgroundColor="grey"
two.style.backgroundColor="#31d2f2"
two.disabled=false
showPage.innerHTML=`${currentPage}/${Math.ceil(getRoomSortMessage1.length/3)}`
renderPage(getRoomSortMessage)
}

function fok(){
    const imgurl=document.querySelector("#rimg").value
    const roomType=document.querySelector("#roomSort").value
    const describe1=document.querySelector("#roomDec").value
    const price=document.querySelector("#roomPrice").value
    if(!(imgurl&&roomType&&describe1&&price)){
        return alert("请输入完整的信息！")
    }
    Ajax({
        url:"http://localhost/hotel/php/roomSortAdd.php",
        data:{imgurl,roomType,describe1,price},
        success(){
            location.reload()
        }
    })
}

const tbody=document.querySelector("tbody")
tbody.onclick=function(e){
    if(e.target.id=='del'){
        const res=confirm("确定要删除吗?")
        if(res){
        Ajax({
            url:"http://localhost/hotel/php/roomSortDelete.php",
            data:{sid:e.target.dataset.roomid},
            success(){
                location.reload()
            }
        })
       
        }
    }

    if(e.target.id=='upd'){
    Ajax({
        url:"http://localhost/hotel/php/oneRoom.php",
        data:{sid:e.target.dataset.roomid},
        success(data){
            data=JSON.parse(data)
            document.querySelector("#updid").value=data.sid
            document.querySelector("#updimg").value=data.imgurl
            document.querySelector("#updroomSort").value=data.roomType
            document.querySelector("#updroomDec").value=data.describe1
            document.querySelector("#updroomPrice").value=data.price
        }
    })
    }
}

function upd(){
    const sid=document.querySelector("#updid").value
    const imgurl=document.querySelector("#updimg").value
    const roomType=document.querySelector("#updroomSort").value
    const describe1=document.querySelector("#updroomDec").value
    const price=document.querySelector("#updroomPrice").value
   
    if(!(imgurl&&roomType&&describe1&&price)){
        return alert("请输入完整的数据!")
    }
    console.log(sid,imgurl,roomType,describe1,price)
    Ajax({
        url:"http://localhost/hotel/php/roomSortUpdate.php",
        data:{sid,imgurl,roomType,describe1,price},
        success(){
            location.reload()
        }
    })
}

function sea(){
    let roomType
    const roomType1=document.querySelectorAll("#sel option")
    for(let i=1;i<roomType1.length;i++){
        if(roomType1[i].selected){
            roomType=roomType1[i].innerHTML
        }
    }
    Ajax({
        url:" http://localhost/hotel/php/roomtypeQuery.php",
        data:{roomType},
        success(data){
            sessionStorage.setItem("getRoomSortMessage",data)
            sessionStorage.setItem("currentPage",1)
            let getRoomSortMessage1=JSON.parse(sessionStorage.getItem("getRoomSortMessage"))
            let currentPage=+sessionStorage.getItem("currentPage")
            showPage.innerHTML=`${currentPage}/${Math.ceil(getRoomSortMessage1.length/3)}`
            data=(JSON.parse(data)).slice(0,3)
            two.style.backgroundColor="#31d2f2"
            two.disabled=false
            three.style.backgroundColor="#31d2f2"
            three.disabled=false
            renderPage(data)
        }
    })
}

function sea1(){
    const price1=document.querySelector("#inp1").value
    const price2=document.querySelector("#inp2").value
    Ajax({
        url:"http://localhost/hotel/php/roompriceQuery.php",
        data:{price1,price2},
        success(data){
            sessionStorage.setItem("getRoomSortMessage",data)
            sessionStorage.setItem("currentPage",1)
            let getRoomSortMessage1=JSON.parse(sessionStorage.getItem("getRoomSortMessage"))
            let currentPage=+sessionStorage.getItem("currentPage")
            showPage.innerHTML=`${currentPage}/${Math.ceil(getRoomSortMessage1.length/3)}`
            two.style.backgroundColor="#31d2f2"
            two.disabled=false
            three.style.backgroundColor="#31d2f2"
            three.disabled=false
            data=(JSON.parse(data)).slice(0,3)
            renderPage(data)
        }
    })
}

